# -*- coding: utf-8 -*-

from django.db import models
from datetime import date


class Empresa(models.Model):
    nombre = models.CharField(max_length=50, null=True)
    rut = models.CharField(max_length=12, blank=True, null=True)
    direccion = models.CharField(max_length=50, blank=True, null=True)
    telefono1 = models.CharField(max_length=15, blank=True, null=True)
    telefono2 = models.CharField(max_length=15, blank=True, null=True)
    email1 = models.CharField(max_length=50, blank=True, null=True)
    email2 = models.CharField(max_length=50, blank=True, null=True)
    saldo_gastos_usd = models.FloatField(blank=True, null=True)
    saldo_gastos_actualizado = models.NullBooleanField(null=True)

    def __unicode__(self):
        return '%s' % self.nombre


class Cotizaciones(models.Model):
    fecha = models.DateField(blank=True, null=True)
    moneda = models.CharField(max_length=3, null=True)
    compra = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
    venta = models.DecimalField(max_digits=5, decimal_places=2, blank=True, null=True)
    interbancario = models.DecimalField(max_digits=5, decimal_places=3, blank=True, null=True)
    
    def __unicode__(self):
        return '%s - %s' % (self.fecha, self.moneda)
    
    class Meta:
        verbose_name_plural = 'Cotizaciones'
        ordering = ['-fecha']


class Clientes(models.Model):
    nombre = models.CharField(max_length=50, null=True)
    apellido = models.CharField(max_length=50, null=True)
    rut = models.CharField(max_length=12, null=True)
    direccion = models.CharField(max_length=50, null=True)
    telefono1 = models.CharField(max_length=15, null=True)
    telefono2 = models.CharField(max_length=15, null=True)
    email1 = models.CharField(max_length=50, null=True)
    email2 = models.CharField(max_length=50, null=True)
    cumple = models.DateField(blank=True, null=True)
    observaciones = models.TextField(null=True)
    saldoUSD = models.FloatField(blank=True, null=True)
    saldo_actualizado = models.NullBooleanField(null=True)

    def __unicode__(self):
        return '%s, %s' % (self.apellido, self.nombre)
    
    class Meta:
        verbose_name_plural = 'Clientes'
        ordering = ['apellido', 'nombre']

    def _get_cuentas(self):
        cuentas = self.cuentasclientes_set.all()
        return cuentas

    def _get_saldo_usd(self):
        return self.saldoUSD


class Rubros(models.Model):
    rubro = models.CharField(max_length=50, blank=True, null=True)
    
    def __unicode__(self):
        return self.rubro
    
    class Meta:
        verbose_name_plural = 'Rubros'


class Gastos(models.Model):
    fecha = models.DateField(blank=True, null=True)
    rubro = models.ForeignKey(Rubros, on_delete=models.PROTECT, blank=True, null=True)
    concepto = models.TextField(null=True)
    moneda = models.CharField(max_length=3, null=True)
    debe = models.FloatField(blank=True, null=True)
    haber = models.FloatField(blank=True, null=True)
    subtotal = models.FloatField(blank=True, null=True)
    saldoUSD = models.FloatField(blank=True, null=True)

    class Meta:
        verbose_name_plural = 'Ingresos y gastos'
        ordering = ['fecha', 'id']
        

class CuentasClientes(models.Model):
    cliente = models.ForeignKey(Clientes, on_delete=models.PROTECT, blank=True, null=True)
    fecha = models.DateField(blank=True, null=True)
    concepto = models.TextField(null=True)
    moneda = models.CharField(max_length=3, null=True)
    debe = models.FloatField(blank=True, null=True)
    haber = models.FloatField(blank=True, null=True)
    subtotal = models.FloatField(blank=True, null=True)
    saldoUSD = models.FloatField(blank=True, null=True)
    
    class Meta:
        verbose_name_plural = 'Ingresos y gastos'
        ordering = ['fecha', 'id']
        

class BancoPesos(models.Model):
    fecha = models.DateField(blank=True, null=True)
    fechaAcreditacion = models.DateField(blank=True, null=True)
    rubro = models.TextField(null=True)
    debe = models.FloatField(blank=True, null=True)
    haber = models.FloatField(blank=True, null=True)
    saldo = models.FloatField(blank=True, null=True)

    @property
    def acreditado(self):
        if self.fechaAcreditacion > date.today():
            return False
        else:
            return True

    class Meta:
        verbose_name_plural = 'Cuenta en Pesos'
        ordering = ['fechaAcreditacion', 'id']
        

class BancoDolares(models.Model):
    fecha = models.DateField(blank=True, null=True)
    fechaAcreditacion = models.DateField(blank=True, null=True)
    rubro = models.TextField(null=True)
    debe = models.FloatField(blank=True, null=True)
    haber = models.FloatField(blank=True, null=True)
    saldo = models.FloatField(blank=True, null=True)

    @property
    def acreditado(self):
        if self.fechaAcreditacion > date.today():
            return False
        else:
            return True

    class Meta:
        verbose_name_plural = 'Cuenta en Dolares'
        ordering = ['fechaAcreditacion', 'id']


class Remitos(models.Model):
    fecha = models.DateField(blank=True, null=True)
    cliente = models.ForeignKey(Clientes, on_delete=models.PROTECT, blank=True, null=True)
    numeroRemito = models.IntegerField(blank=True, null=True)
    
    class Meta:
        verbose_name_plural = 'Remitos'
        ordering = ['numeroRemito']


class ItemsRemitos(models.Model):
    remito = models.ForeignKey(Remitos, on_delete=models.PROTECT, blank=True, null=True)
    numeroLinea = models.IntegerField(blank=True, null=True)
    descripcion = models.TextField(null=True)
    base = models.FloatField(blank=True, null=True)
    estado = models.CharField(max_length=10, null=True)
    
    class Meta:
        verbose_name_plural = 'Items de Remito'
        ordering = ['estado', 'numeroLinea']
        

class Remates(models.Model):
    fecha = models.DateField(blank=True, null=True)
    observaciones = models.TextField(null=True)
    
    class Meta:
        verbose_name_plural = 'Remates'
        ordering = ['fecha']


class ItemsRemates(models.Model):
    remate = models.ForeignKey(Remates, on_delete=models.PROTECT, blank=True, null=True)
    articulo = models.ForeignKey(ItemsRemitos, on_delete=models.PROTECT, blank=True, null=True)
    numeroLote = models.IntegerField(blank=True, null=True)
    precioVenta = models.FloatField(blank=True, null=True)
    detalle = models.TextField(null=True)
    comprador = models.ForeignKey(Clientes, on_delete=models.PROTECT, blank=True, null=True)
    vendido = models.BooleanField()
    
    class Meta:
        verbose_name_plural = 'Items de Remate'
        ordering = ['remate', 'numeroLote']
